/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.config;

import com.jspx.sober.annotation.Column;
import com.jspx.sober.annotation.Table;
import com.jspx.txweb.annotation.Operate;
import com.jspx.utils.StringUtil;

import java.util.*;
import java.io.Serializable;


/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2006-12-26
 * Time: 20:05:27
 * com.jspx.txweb.config.ActionConfigBean
 * TXWeb action 配置对象
 */
@Table(name = "jspx_action", caption = "TXWeb配置", create = false, cache = true)
public class ActionConfigBean implements ActionConfig, Serializable {
    @Column(caption = "名称", length = 200, notNull = true)
    private String caption = StringUtil.empty;
    @Column(caption = "支持类型", option = "class,ajax", length = 200, notNull = true)
    private String type = StringUtil.empty;
    @Column(caption = "对象名称", length = 200, notNull = true)
    private String actionName;
    @Column(caption = "ioc对象", length = 200, notNull = true)
    private String iocBean;
    @Column(caption = "操作类", length = 200, dataType = "isLengthBetween(2,200)", notNull = true)
    private String className = StringUtil.empty;
    @Column(caption = "执行方法", length = 200)
    private String method;
    @Column(caption = "手机支持", notNull = true)
    private boolean mobile = false;
    @Column(caption = "RESTFull调用路径", length = 250)
    private String location = StringUtil.empty;

    //判断是否为加密传输
    @Column(caption = "保密", notNull = true)
    private boolean secret = false;

    private Map<String, Object> param = new HashMap<String, Object>();
    private List<String> interceptors = new LinkedList<String>();
    private List<ResultConfigBean> resultConfigs = new ArrayList<ResultConfigBean>();
    private String[] passInterceptor = null;
    private Map<String, Operate> operateMap = new LinkedHashMap<String, Operate>();


    public ActionConfigBean() {

    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public String getActionName() {
        return actionName;
    }

    public void setActionName(String actionName) {
        this.actionName = actionName;
    }

    public String getIocBean() {
        return iocBean;
    }

    void setIocBean(String iocBean) {
        this.iocBean = iocBean;
    }


    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }


    public String getMethod() {
        return method;
    }

    public void setMethod(String method) throws Exception {
        this.method = method;
    }

    void addParam(String name, Object value) {
        param.put(name, value);
    }

    public Object getParam(String name) {
        return param.get(name);
    }

    public Map<String, Object> getParam() {
        return param;
    }

    public String[] getPassInterceptor() {
        return passInterceptor;
    }

    void setPassInterceptor(String[] passInterceptor) {
        this.passInterceptor = passInterceptor;
    }

    public List<String> getInterceptors() {
        return interceptors;
    }

    void addInterceptors(String interceptorName) {
        interceptors.add(interceptorName);
    }

    public ResultConfigBean getResultConfig(String name) {
        if (StringUtil.isNULL(name)) name = "*";
        for (ResultConfigBean resultConfigBean : resultConfigs) {
            if (resultConfigBean.getName().equals(name)) {
                return resultConfigBean;
            }
        }
        if (!"*".equals(name)) {
            for (ResultConfigBean resultConfigBean : resultConfigs) {
                if (resultConfigBean.getName().equals("*")) {
                    return resultConfigBean;
                }
            }
        }
        return null;
    }


    public List<ResultConfigBean> getResultConfigs() {
        return resultConfigs;
    }

    void addResultConfig(ResultConfigBean resultConfig) {
        resultConfigs.add(resultConfig);
    }

    public Map<String, Operate> getOperateMap() {
        return operateMap;
    }

    public boolean isMobile() {
        return mobile;
    }

    public void setMobile(boolean mobile) {
        this.mobile = mobile;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public boolean isInit() {
        return !operateMap.isEmpty();
    }

    public boolean isSecret() {
        return secret;
    }

    public void setSecret(boolean secret) {
        this.secret = secret;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("<action name=\"").append(actionName).append("\" ");
        if (!StringUtil.isNULL(caption))
            sb.append("caption=\"").append(caption).append("\" ");
        sb.append(" class=\"").append(iocBean).append("\" ");
        if (!StringUtil.isNULL(method))
            sb.append("method=\"").append(method).append("\" ");
        if (secret)
            sb.append("secret=\"").append(Boolean.toString(secret)).append("\" ");
        if (mobile)
            sb.append("mobile=\"").append(Boolean.toString(mobile)).append("\" ");
        sb.append(">\r\n");
        if (param != null)
            for (String pkey : param.keySet())
                sb.append("<param name=\"").append(pkey).append("\">").append(param.get(pkey)).append("</param>\r\n");
        if (resultConfigs != null)
            for (ResultConfigBean resultConfigBean : resultConfigs)
                sb.append(resultConfigBean.toString());
        if (interceptors != null)
            for (String interceptor : interceptors)
                sb.append("<interceptor-ref name=\"").append(interceptor).append("\" />\r\n");
        sb.append("</action>\r\n");
        return sb.toString();
    }
}